package s

import ("github.com/maxwellhealth/bongo"
	"gopkg.in/mgo.v2/bson"

	"www2/util"
	"log"
)

var collection *bongo.Collection
func init(){
//	config := &bongo.Config{
//		ConnectionString: "localhost",
//		Database:         "bongotest",
//	}
//	connection, _ := bongo.Connect(config)
 	collection=util.Bconn.Collection("prods")
}

func GetProdByID(ID string )(*Prod,error){
	prod := &Prod{}
	err := collection.FindById(bson.ObjectIdHex(ID), prod)
	return prod,err
}

func DelteProdByID(ID string )(error){
	prod := &Prod{}
	err := collection.FindById(bson.ObjectIdHex(ID), prod)
	if err==nil{
		err=collection.DeleteDocument(prod)
	}
	return err
}

func Save(prod *Prod) error{
	err := collection.Save(prod)
	return err
}

func FindByCon(perPage, page int,con map[string]interface{}) ([]ProdV,error){
	results := collection.Find(bson.M(con))
	pageinfo,err:=results.Paginate(perPage,page)
	prods:=[]ProdV{}
	if err==nil{
		results.Query.Skip((pageinfo.Current - 1) * pageinfo.PerPage).Limit(perPage).All(&prods)
	}
	log.Println("prods1:",prods)
	return prods,err

}

func GetF() bongo.Document{
	return &Prod{}
}



